package org.boulderfoodrescue.webapp.client.old.calendar.ical;

import java.util.Date;

import org.boulderfoodrescue.webapp.client.TabPanel;
import org.boulderfoodrescue.webapp.client.resources.Resources;

import com.bradrydzewski.gwt.calendar.client.Appointment;
import com.bradrydzewski.gwt.calendar.client.Calendar;
import com.bradrydzewski.gwt.calendar.client.CalendarSettings;
import com.bradrydzewski.gwt.calendar.client.CalendarSettings.Click;
import com.bradrydzewski.gwt.calendar.client.CalendarViews;
import com.bradrydzewski.gwt.calendar.client.event.DeleteEvent;
import com.bradrydzewski.gwt.calendar.client.event.DeleteHandler;
import com.bradrydzewski.gwt.calendar.client.event.TimeBlockClickEvent;
import com.bradrydzewski.gwt.calendar.client.event.TimeBlockClickHandler;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.datepicker.client.DatePicker;

public class iCalCalendarPanel extends DockLayoutPanel implements TabPanel{

  private iCalCalendarPanel calendarPanel;
  private Calendar calendar = null;       
  private DatePicker datePicker = new DatePicker();
  private FlowPanel headerPanel = new FlowPanel();
  private FlexTable headerPanelLayout = new FlexTable();
  private FlowPanel footerPanel = new FlowPanel();
  private FlowPanel dateLayoutPanel = new FlowPanel();
  private Button todayButton = new Button();
  private Button nextDayButton = new Button();
  private Button previousDayButton = new Button();
  private Button oneDayButton = new Button();
  private Button threeDayButton = new Button();
  private Button weekDayButton = new Button();
  private Button monthButton = new Button();
  private Button activeDayButton = null;
  private long days = 3;

  private CalendarSettings settings = new CalendarSettings();

  public iCalCalendarPanel() {
    super(Unit.PX);
    final Resources resources = GWT.create(Resources.class);
    calendarPanel = this;
    //add header
    headerPanel.setStyleName(resources.calendarCss().gwtCalHeaderPanel());
    DOM.setInnerHTML(headerPanel.getElement(), "&nbsp;");
    footerPanel.setStyleName(resources.calendarCss().gwtCalFooterPanel());
    DOM.setInnerHTML(headerPanel.getElement(), "&nbsp;");
    this.addNorth(headerPanel, 47);
    this.addSouth(footerPanel, 25);
    
    //add left panel
    datePicker.setValue(new Date());
    dateLayoutPanel.add(datePicker);
    this.addWest(dateLayoutPanel, 206);

    settings.setOffsetHourLabels(true);
    settings.setTimeBlockClickNumber(Click.Single);
    calendar = new Calendar();
    calendar.setSettings(settings);
    calendar.setWidth("100%");
    this.add(calendar);

    //add today button
    todayButton.setStyleName(resources.calendarCss().todayButton());
    todayButton.setText("Today");
    todayButton.addClickHandler(new ClickHandler(){
      @Override
      public void onClick(ClickEvent event) {
        datePicker.setValue(new Date(), true);
        datePicker.setCurrentMonth(new Date());
      }
    });
    previousDayButton.setStyleName(resources.calendarCss().previousButton());
    previousDayButton.setHTML("&laquo;");
    previousDayButton.addClickHandler(new ClickHandler(){
      @Override
      public void onClick(ClickEvent event) {
        Date d = datePicker.getValue();
        Date newd = new Date(d.getTime()-24*60*60*1000*days);
        datePicker.setValue(newd,true);
        datePicker.setCurrentMonth(newd);
      }
    });
    nextDayButton.setStyleName(resources.calendarCss().nextButton());
    nextDayButton.setHTML("&raquo;");
    nextDayButton.addClickHandler(new ClickHandler(){
      @Override
      public void onClick(ClickEvent event) {
        Date d = datePicker.getValue();
        Date newd = new Date(d.getTime()+24*60*60*1000*days);
        datePicker.setValue(newd,true);
        datePicker.setCurrentMonth(newd);
      }
    });
    headerPanelLayout.setWidget(0,0,todayButton);
    headerPanelLayout.setWidget(0,1,previousDayButton);

    oneDayButton.setText("1 Day");
    oneDayButton.setStyleName(resources.calendarCss().dayButton());
    threeDayButton.setText("3 Day");
    threeDayButton.setStyleName(resources.calendarCss().dayButton());
    threeDayButton.addStyleName(resources.calendarCss().active());
    activeDayButton = threeDayButton;
    weekDayButton.setText("Work Week");
    weekDayButton.setStyleName(resources.calendarCss().dayButton());
    monthButton.setText("Month");
    monthButton.setStyleName(resources.calendarCss().dayButton());
    headerPanelLayout.setWidget(0,2,oneDayButton);
    headerPanelLayout.setWidget(0,3,threeDayButton);
    headerPanelLayout.setWidget(0,4,weekDayButton);
    headerPanelLayout.setWidget(0,5,monthButton);
    headerPanelLayout.setWidget(0,6,nextDayButton);
    headerPanelLayout.setHTML(0, 7, "&nbsp;");

    headerPanelLayout.getCellFormatter().setWidth(0, 0, "50%");
    headerPanelLayout.getCellFormatter().setWidth(0, 7, "50%");

    headerPanelLayout.setWidth("100%");
    headerPanelLayout.setCellPadding(0);
    headerPanelLayout.setCellSpacing(0);
    headerPanel.add(headerPanelLayout);

    footerPanel.add(new HTML("<a href='http://code.google.com/p/gwt-cal'>gwt-cal</a> widget for Google Web Toolkit, GPLv3, by <a href='http://www.google.com/profiles/Brad.Rydzewski'>Brad Rydzewski</a> modified by <a href='http://www.sean-wiese.com'>Sean Wiese</a>"));

    oneDayButton.addClickHandler(new ClickHandler(){
      @Override
      public void onClick(ClickEvent event) {
        activeDayButton.removeStyleName(resources.calendarCss().active());
        activeDayButton = oneDayButton;
        activeDayButton.addStyleName(resources.calendarCss().active());
        calendar.setView(CalendarViews.DAY, 1);
        days = 1;
        //calendar.scrollToHour(6);
      }
    });
    threeDayButton.addClickHandler(new ClickHandler(){
      @Override
      public void onClick(ClickEvent event) {
        activeDayButton.removeStyleName(resources.calendarCss().active());
        activeDayButton = threeDayButton;
        activeDayButton.addStyleName(resources.calendarCss().active());
        calendar.setView(CalendarViews.DAY, 3);
        days = 3;
        //calendar.scrollToHour(6);
      }
    });
    weekDayButton.addClickHandler(new ClickHandler(){
      @Override
      public void onClick(ClickEvent event) {
        activeDayButton.removeStyleName(resources.calendarCss().active());
        activeDayButton = weekDayButton;
        activeDayButton.addStyleName(resources.calendarCss().active());
        calendar.setView(CalendarViews.DAY, 5);
        days = 7;
        //calendar.scrollToHour(6);
      }
    });
    monthButton.addClickHandler(new ClickHandler(){
      @Override
      public void onClick(ClickEvent event) {
        activeDayButton.removeStyleName(resources.calendarCss().active());
        activeDayButton = monthButton;
        activeDayButton.addStyleName(resources.calendarCss().active());
        calendar.setView(CalendarViews.MONTH);
        days = 31;
      }
    });

    datePicker.addValueChangeHandler(new ValueChangeHandler<Date>(){
      @Override
      public void onValueChange(ValueChangeEvent<Date> event) {
        calendar.setDate(event.getValue());
      }
    });
    calendar.addDeleteHandler(new DeleteHandler<Appointment>(){

      @Override
      public void onDelete(DeleteEvent<Appointment> event) {
        boolean commit = Window.confirm("Are you sure you want to delete appointment \"" + event.getTarget().getTitle() + "\"");
        if(commit==false) {
          event.setCancelled(true);
          System.out.println("cancelled appointment deletion");
        }

      }

    });
    calendar.addOpenHandler(new OpenHandler<Appointment>(){
      @Override
      public void onOpen(OpenEvent<Appointment> event) {
        Window.alert("You double-clicked appointment \"" + event.getTarget().getTitle() + "\"");
      }

    });

    calendar.addSelectionHandler(new SelectionHandler<Appointment>(){

      @Override
      public void onSelection(SelectionEvent<Appointment> event) {
        System.out.println("selected " + event.getSelectedItem().getTitle());
      }

    });

    calendar.addTimeBlockClickHandler(new TimeBlockClickHandler<Date>(){

      @Override
      public void onTimeBlockClick(TimeBlockClickEvent<Date> event) {
        Appointment appt = new Appointment();
        appt.setStart(event.getTarget());
        appt.setEnd(new Date(event.getTarget().getTime()+60*60*1000));
        NewEventDialog eDialog = new NewEventDialog(calendar, appt);
        eDialog.center();
      }
    });
    /* Add appointments to day view */
    calendar.suspendLayout();
    
    calendar.resumeLayout();

    calendar.setHeight("100%");
    calendar.scrollToHour(7);
    Timer t = new Timer(){
      @Override
      public void run() {
        calendarPanel.setWidgetSize(dateLayoutPanel, datePicker.getOffsetWidth());
      }
    };
    t.schedule(100);
  }

  @Override
  public void resized() {
    calendarPanel.setWidgetSize(dateLayoutPanel, datePicker.getOffsetWidth());
  }
}
